﻿using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Threading.Tasks;
using BookSourceVerification.execute;
using BookSourceVerification.service;
using BookSourceVerification.service.impl;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace BookSourceVerification.test;

public class TestDemo
{
    public static void Test06()
    {
        ServiceProvider serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider();
        var httpClientFactory = serviceProvider.GetService<IHttpClientFactory>()!;
        var httpClient = httpClientFactory.CreateClient();
        var task = httpClient.GetAsync("https://www.baidu.com");
        Console.WriteLine(task.Result.StatusCode);
    }


    public static void Test04()
    {
        var arr = new int[200];
        var impl = new CheckSourceHttpServiceImpl();
        var startNew = Stopwatch.StartNew();
        var task = Parallel.ForEachAsync(arr, async (i, token) => { await impl.Check("https://www.baidu.com"); });
        task.Wait();
        startNew.Stop();
        Console.WriteLine("wwww:" + startNew.ElapsedMilliseconds);
    }


    public static void Test03()
    {
        var stopwatch = new Stopwatch();
        stopwatch.Start();
        Parallel.For(0, 100, _ => { ThreadTest(); });
        stopwatch.Stop();
        Console.WriteLine(stopwatch.ElapsedMilliseconds);
    }

    private static void ThreadTest()
    {
        using var httpClient = new HttpClient();
        httpClient.Timeout = TimeSpan.FromMilliseconds(1000);
        var task = httpClient.GetAsync("http://www.baidu.com");
        Console.WriteLine(task.Result.StatusCode);
    }

    public static async void Test02()
    {
        await using var stream = File.OpenRead(@"C:\Users\zhazha\Desktop\test.json");
        // await using var stream = File.OpenRead(@"C:\Users\zhazha\Desktop\724条书源.json");
        await using var writer = File.CreateText(@"C:\Users\zhazha\Desktop\success.json");
        var document = await JsonDocument.ParseAsync(stream);
        var rootElement = document.RootElement;
        if (rootElement.ValueKind != JsonValueKind.Array)
        {
            throw new JsonException("书源json格式错误，书源必须是数组");
        }

        var jsonWriterOptions = new JsonWriterOptions
        {
            Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
        };
        await using var jsonWriter = new Utf8JsonWriter(writer.BaseStream, jsonWriterOptions);
        jsonWriter.WriteStartArray();
        foreach (var jsonElement in rootElement.EnumerateArray())
        {
            jsonElement.WriteTo(jsonWriter); // 将 jsonElement 写入到 JSON 数组中
        }

        jsonWriter.WriteEndArray();
        var flushAsync = jsonWriter.FlushAsync();
        flushAsync.Wait();
    }


    public static void Test01()
    {
        // var checkBookSourceService = new CheckSourceHttpServiceImpl();
        // var verifyBookSourceExecute = new VerifyBookSourceExecute(checkBookSourceService);
        // Task<JsonDocument> task = VerifyBookSourceExecute.LoadJson(@"C:\Users\zhazha\Desktop\724条书源.json");
        // using var jsonDocument = task.Result;
        // foreach (var jsonElement in jsonDocument.RootElement.EnumerateArray())
        // {
        // }
        //
        // Console.WriteLine("hello world");
    }
}